package com.mogu.comment.constraint;

import com.mogu.comment.annotion.Range;
import com.mogu.comment.utils.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Description:不在能知，而在能行
 *
 * @author: 梁西华
 * @date: 2020/6/11/14:21
 **/

public class RangValidator implements ConstraintValidator<Range,String>{

    private long min;
    private long max;
    private String type;

    @Override
    public void initialize(Range constraintAnnotation) {

        this.min= constraintAnnotation.min();
        this.max = constraintAnnotation.max();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null || StringUtils.isBlank(value)){
            return false;
        }
        return value.length() >= min && value.length() <= max;
    }
}
